import { createRouter, createWebHistory } from 'vue-router';
import menuStore from '@/stores/framework/menu.js';

import Index from '../views/Index.vue';
import Login from '@/views/Login';
import Redirect from '@/views/Redirect';
import Home from '@/views/Home';
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },

  {
    name: '404',
    path: '/:catchAll(.*)',
    component: Login
  },
  {name: "重定向地址", component: Redirect, path: "/redirect", meta: {layout: false}},
  {
    path: '/',
    name: 'Enter',
    component: Index,
    redirect: '/login',
    children: [
      {
        path: 'home',
        name: 'Home',
        component: Home,
      },
      // {meta: {title: "首页",history: false}, path: '/home', name: 'home',  component: () => import('../views/Home.vue')},

      // {meta: {history: true, title: "用户管理"}, path: '/user/index', name: 'user', component: () => import('../views/user/Index.vue')},
      //
      // {meta: {history: true, title: "社区管家管理"}, path: '/community/index', name: 'community', component: () => import('../views/community/Index.vue')},
      //
      // {meta: {history: true, title: "服务项目档案"}, path: '/project/index', name: 'project', component: () => import('../views/project/Index.vue')},
      // {meta: {history: true, title: "服务项目上架"}, path: '/project/shop', name: 'project-shop', component: () => import('../views/project/shop/Index.vue')},
      //
      //
      // {meta: {history: true, title: "医院管理"}, path: '/base/hospital', name: 'hospital', component: () => import('../views/base/hospital/Index.vue')},
      // {meta: {history: true, title: "分类管理"}, path: '/base/category', name: 'category', component: () => import('../views/base/category/Index.vue')},
      // {meta: {history: true, title: "轮播图管理"}, path: '/base/banner', name: 'banner', component: () => import('../views/base/banner/Index.vue')},
      //
      // {meta: {history: true, title: "用户管理"}, path: '/sys/user', name: 'sys-user', component: () => import('../views/sys/user/Index.vue')},
      // {meta: {history: true, title: "角色管理"}, path: '/sys/role', name: 'role', component: () => import('../views/sys/role/Index.vue')},
      // {meta: {history: true, title: "字典管理"}, path: '/sys/dict', name: 'dict', component: () => import('../views/sys/dict/Index.vue')},
      // {meta: {history: true, title: "字典数据"}, path: '/sys/dict-data', name: 'dict-data', component: () => import('../views/sys/dictData/Index.vue')},
      // {meta: {history: true, title: "参数设置"}, path: '/sys/parameter', name: 'parameter', component: () => import('../views/sys/parameter/Index.vue')},
      // {meta: {history: true, cache: true, title: "菜单管理"}, path: '/sys/menu', name: 'menu', component: () => import('../views/sys/menu/Index.vue')},
    ]
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

router.init = async (store, cb) => {
  const {menu} = store();
  await menu.init();
  // router.addRoute({meta: {history: true, title: "菜单管理"}, path: '/sys/menu', name: 'menu', component: () => import('@/views/sys/menu/Index.vue')});
  cb && cb();
}

router.beforeEach((to, from, next) => {
  const menu = menuStore();
  if(to.meta.history === true) {
    Cache.dispatch("menu", 'addHistory', to);
  }

  next()
})

export default router
